#include "multirobotcontroldlg.h"
#include "ui_multirobotcontroldlg.h"

#include "communicatedelegate.h"
#include <QMessageBox>


MultiRobotControlDlg::MultiRobotControlDlg(CommunicateNormalDelegate *communicateNormalIn, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MultiRobotControlDlg)
{
    ui->setupUi(this);
    communicateNormal=communicateNormalIn;
//    setGeometry(70,128,460,595);
    move(0,0);
    setStyleSheet("#frame{border:2px solid #014F84}#background-color:#00d8ff");
//    this->setWindowFlags(Qt::FramelessWindowHint);    //无边框
//    this->setStyleSheet("outline: none;");            //无虚线框
//    setWindowFlags( Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
//    setWindowFlags( Qt::FramelessWindowHint | Qt::Tool );

    timer=new QTimer();
    connect( timer, SIGNAL(timeout()), this, SLOT(timeOutSlot()) );

    model_taskInfo = new QStandardItemModel();
//    QStringList labels3 = QObject::trUtf8("变量,value,-").simplified().split(",");
//    model_taskInfo->setHorizontalHeaderLabels(labels3);
//    ui->tableView_allRobotTask->setColumnWidth(1,120);

    model_taskInfo->setColumnCount(4);
    ui->tableView_allRobotTask->setColumnWidth(0,120);
    ui->tableView_allRobotTask->setColumnWidth(1,50);
    ui->tableView_allRobotTask->setColumnWidth(2,200);
    ui->tableView_allRobotTask->setColumnWidth(3,80);
    model_taskInfo->setHeaderData(0,Qt::Horizontal,QString::fromLocal8Bit("ROBOT"));
    model_taskInfo->setHeaderData(1,Qt::Horizontal,QString::fromLocal8Bit("TASK"));
    model_taskInfo->setHeaderData(2,Qt::Horizontal,QString::fromLocal8Bit("PROGRAM"));
    model_taskInfo->setHeaderData(3,Qt::Horizontal,QString::fromLocal8Bit("LINE"));
//    model_taskInfo->setHeaderData(0,Qt::Horizontal,"ROBOT");
//    model_taskInfo->setHeaderData(1,Qt::Horizontal,"TASK");
//    model_taskInfo->setHeaderData(2,Qt::Horizontal,"PROGRAM");
//    model_taskInfo->setHeaderData(3,Qt::Horizontal,"LINE");
    ui->tableView_allRobotTask->verticalHeader()->hide();

    ui->tableView_allRobotTask->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择模式，选择单行
    ui->tableView_allRobotTask->setEditTriggers(QAbstractItemView::NoEditTriggers);

    setAttribute(Qt::WA_QuitOnClose, false);
}

MultiRobotControlDlg::~MultiRobotControlDlg()
{
    delete ui;
}

void MultiRobotControlDlg::showOneRobotTaskInfo(TaskManagerInfo taskInfo,int startLine,int &nextStartLine)
{
    //定义item
    QStandardItem* item = 0;

    int lineSize=1;
    nextStartLine=startLine;

    for(int i=0;i<taskInfo.runningLine.size();i++)
    {
        if(1==taskInfo.state[i])
        {
            item = new QStandardItem(QString::fromStdString(taskInfo.robotName));
            model_taskInfo->setItem(startLine+lineSize*i+0,0,item);


            item = new QStandardItem(QString::number(taskInfo.taskIndex[i]));
            model_taskInfo->setItem(startLine+lineSize*i+0,1,item);


            item = new QStandardItem(QString("%1").arg(
                                         QString::fromStdString(taskInfo.fileName[i])));
            model_taskInfo->setItem(startLine+lineSize*i+0,2,item);


            item = new QStandardItem(QString::number(taskInfo.runningLine[i],'f',3));
            model_taskInfo->setItem(startLine+lineSize*i+0,3,item);

            nextStartLine++;
        }


    }


}


void MultiRobotControlDlg::showThisDialog()
{
    move(0,0);
    timer->start( 500 );
    show();

}

void MultiRobotControlDlg::timeOutSlot()
{
    if(false==ui->checkBox_upDateEnable->isChecked())
    {
        return ;
    }
    std::vector<struct TaskManagerInfo> taskManagerInfo;
    communicateNormal->getAllTaskInfo(taskManagerInfo);

    model_taskInfo->clear();

    QStandardItem* item = 0;

    item = new QStandardItem("机器人");
    model_taskInfo->setItem(0,0,item);
    item = new QStandardItem("任务号");
    model_taskInfo->setItem(0,1,item);
    item = new QStandardItem("程序");
    model_taskInfo->setItem(0,2,item);
    item = new QStandardItem("行号");
    model_taskInfo->setItem(0,3,item);

    int startLine=1;
    for(int i=0;i<taskManagerInfo.size();i++)
    {
         showOneRobotTaskInfo(taskManagerInfo[i],startLine,startLine);

    }
    ui->tableView_allRobotTask->setModel(model_taskInfo);
    ui->tableView_allRobotTask->resizeColumnToContents(0);
    ui->tableView_allRobotTask->resizeColumnToContents(1);
    ui->tableView_allRobotTask->resizeColumnToContents(2);
    ui->tableView_allRobotTask->show();
}

void MultiRobotControlDlg::on_comboBox_allModel_currentIndexChanged(int index)
{
    int hr = QMessageBox::warning( this, "Warning",    "是否切换全部机器人的控制模式!",
                                   QMessageBox::Yes|QMessageBox::No);//提示框
    if (QMessageBox::Yes == hr)
    {
        int returnFlag;
        communicateNormal->setAllRunModel(index,returnFlag);

    }

}

void MultiRobotControlDlg::on_checkBox_allServOn_clicked(bool checked)
{
    int hr = QMessageBox::warning( this, "Warning",    "是否切使能全部机器人!",
                                   QMessageBox::Yes|QMessageBox::No);//提示框
    if (QMessageBox::Yes == hr)
    {
        int returnFlag;
        communicateNormal->setAllManualServon(!checked,returnFlag);

    }

}

void MultiRobotControlDlg::on_pushButton_allRun_clicked()
{
    int hr = QMessageBox::warning( this, "Warning",    "是否运行全部机器人程序!",
                                   QMessageBox::Yes|QMessageBox::No);//提示框
    if (QMessageBox::Yes == hr)
    {
        int returnFlag;
        communicateNormal->programAllRun(returnFlag);

    }

}

void MultiRobotControlDlg::on_pushButton_allStop_clicked()
{
    int returnFlag;
    communicateNormal->programAllReset(returnFlag);
}

void MultiRobotControlDlg::on_pushButton_all_cease_clicked()
{
    int returnFlag;
    communicateNormal->programAllCease(returnFlag);
}

void MultiRobotControlDlg::on_pushButton_hide_clicked()
{

        timer->stop();
        hide();
}

void MultiRobotControlDlg::showAllRobotTaskInfo(TaskManagerInfo taskInfo)
{

}
